(function ({form, table, layer, laydate, tableIns, url, contentType, ok, AjaxFn, tablebtn, startAndForbid, updataOpen, radioAndAelectData}) {

    radioAndAelectData = ((obj) => (obj = AjaxFn(url + "radioAndAelectData", '', {}), {factorys = '', op = '<option value=""></option>', appraises} = obj, $.each(factorys, (i, item) => op += `<option value="${item.scode}">${item.vname}</option>`), $("select[name=vFactoryFlg]").html(op), op = '<option value=""></option>', $.each(appraises, (i, item) => op += `<option value="${item.scode}">${item.vname}</option>`), $("select[name=vAppraiseFlg]").html(op)))({}), form.render(),

    positionTableIns = table.render({
        elem: '#positionTable',
        url: url + "getPositionTableList",
        method: 'post',
        contentType,
        page: {
            first: '首页',
            last: '尾页',
            prev: '上一页',
            next: '下一页',
            layout: ['count','prev', 'page', 'next'],
            groups: 5,
            theme: '#1e9fff',
            limits: [10, 20, 30, 40, 50],
        },
        cols: [
            [
                {title: '序号', type: 'numbers', fixed: "left", unresize: true, align: "center", minWidth: 100},
                {
                    title: '操作',
                    fixed: "left",
                    unresize: true,
                    align: "center",
                    width: 155,
                    minWidth: 155,
                    templet: function (d, updata = "", startAndForbid = "") {
                        updata = tablebtn({
                            "event": "positionUpdata",
                            "btnclass": "layui-btn layui-btn-xs updata-btn-color",
                            "icon": '&#xe642;',
                            "text": '修改',
                            "disabled": false
                        });
                        startAndForbid = d.delflg == 1 ?
                            tablebtn({
                                "event": "positionStart",
                                "btnclass": "layui-btn layui-btn-xs start-btn-color",
                                "icon": '&#xe683;',
                                "text": '启用',
                                "disabled": false
                            }) :
                            tablebtn({
                                "event": "positionForbid",
                                "btnclass": "layui-btn layui-btn-xs forbid-btn-color",
                                "icon": '&#xe673;',
                                "text": '禁用',
                                "disabled": false
                            });
                        return `<div>${updata}${startAndForbid}</div>`
                    }
                },
                {
                    field: 'vpositionname',
                    title: '状态',
                    unresize: true,
                    align: "center",
                    width:80,minWidth: 80,
                    templet: function (d) {
                        return d.delflg == 1 ? "禁用" : "启用";
                    }
                },
                {field: 'vpositionname', title: '职位名称', unresize: true, align: "center", minWidth: 100},
                {field: 'vinsdate', title: '录入时间', unresize: true, align: "center", width:110,minWidth: 110,}
            ]
        ],
        data: []
    }),
    teamTableIns = table.render({
        elem: '#teamTable',
        url: url + "getTeamTableList",
        method: 'post',
        contentType,
        page: {
            first: '首页',
            last: '尾页',
            prev: '上一页',
            next: '下一页',
            layout: ['count','prev', 'page', 'next'],
            groups: 5,
            theme: '#1e9fff',
            limits: [10, 20, 30, 40, 50],
        },
        cols: [
            [
                {title: '序号', type: 'numbers', fixed: "left", unresize: true, align: "center",width:50,minWidth: 50,},
                {
                    title: '操作',
                    fixed: "left",
                    unresize: true,
                    align: "center",
                    width:155,minWidth: 150,
                    templet: function (d, teamupdata = '', teamstartAndForbid = '') {
                        teamupdata = tablebtn({
                            "event": "teamUpdata",
                            "btnclass": "layui-btn layui-btn-xs updata-btn-color",
                            "icon": '&#xe642;',
                            "text": '修改',
                            "disabled": false
                        });
                        teamstartAndForbid = d.delflg == '1' ?
                            tablebtn({
                                "event": "teamStart",
                                "btnclass": "layui-btn layui-btn-xs start-btn-color",
                                "icon": '&#xe683;',
                                "text": '启用',
                                "disabled": false
                            }) :
                            tablebtn({
                                "event": "teamForbid",
                                "btnclass": "layui-btn layui-btn-xs forbid-btn-color",
                                "icon": '&#xe673;',
                                "text": '禁用',
                                "disabled": false
                            });
                        return '<div>' + teamupdata + teamstartAndForbid + '</div>';
                    }
                },
                {field: 'del_flgname', title: '状态', unresize: true, align: "center", width:80,minWidth: 80,},
                {field: 'vteamname', title: '班组名称', unresize: true, align: "center", minWidth: 100},
                {field: 'vappraiseflgname', title: '评定组', unresize: true, align: "center", width:80,minWidth: 80,},
                {field: 'vfactoryflgname', title: '工厂', unresize: true, align: "center", width:100,minWidth: 100,},
                {field: 'vinsdate', title: '录入时间', unresize: true, align: "center", width:110,minWidth: 110,}
            ]
        ]
    }),
    table.on("tool(positionTable)", function ({data, event, tr}) {
        /*event = positionUpdata/positionStart/positionForbid */
        event == "positionUpdata" ? (function () {
            updataOpen({
                url: url + "positionUpdataHtml/" + data.vpositionid + "/" + data.vpositionname,
                skin: 'positionUpdata',
                title: '职位修改',
                tableIns: positionTableIns
            })
        })() : event == "positionStart" ? (function () {
            startAndForbid({
                url: "startAndForbid",
                startAndForbid: '启用',
                masg: '确定启用该职位吗？',
                btns: ['取消', '启用'],
                param: data,
                tableIns: positionTableIns
            })
        })() : event == "positionForbid" ? startAndForbid({
            url: "startAndForbid",
            startAndForbid: '禁用',
            masg: '确定禁用该职位吗？',
            btns: ['取消', '禁用'],
            param: data,
            tableIns: positionTableIns
        }) : '';
    }),

    table.on("tool(teamTable)", function ({data, event, tr, strdata = ''}) {
        event == "teamUpdata" ? (function () {
            for (let i in data) strdata += i + '=' + data[i] + '&';
            updataOpen({
                url: url + "teamUpdataHtml?" + strdata.substring(0, strdata.length - 1),
                skin: 'teamUpdata',
                title: '班组修改',
                tableIns: teamTableIns
            })
        })() : event == "teamStart" ? (function () {
            startAndForbid({
                url: "teamStartAndForbid",
                startAndForbid: '启用',
                masg: '确定启用该班组吗？',
                btns: ['取消', '启用'],
                param: data,
                tableIns: teamTableIns
            })
        })() : event == "teamForbid" ? (function () {

            startAndForbid({
                url: "teamStartAndForbid",
                startAndForbid: '禁用',
                masg: '确定禁用该班组吗？',
                btns: ['取消', '禁用'],
                param: data,
                tableIns: teamTableIns
            })
        })() : '';
    }),

    form.on('submit(positionSubmit)', function ({field}) {
        field.vPositionName=field.vPositionName.trim()
        let {msg, code} = AjaxFn(url + "positionadd", JSON.stringify(field), {});
        code == 200 ? (layer.msg(msg, {icon: 1}),positionTableIns.reload(),form.val('positionTable',{vPositionName:""})) : (layer.msg(msg, {icon: 5}));
        return false;
    })

    $("#teamAdd").click(c => (c.url = url + 'getTeamAddHtml', c.skin = 'teamAdd', c.title = '班组录入', c.tableIns = teamTableIns, c.btns = ['取消', '新增'], updataOpen(c))),
    $("#beforeteamAdd").click(() => ($("input[name=vteamname]").attr("lay-verify", "required"), $("select[name=vAppraiseFlg]").attr("lay-verify", "required"), $("select[name=vFactoryFlg]").attr("lay-verify", "required"), $("#teamAddSubmit").click())),
    $("#beforeteamSelect").click(() => ($("input[name=vteamname]").attr("lay-verify", ""), $("select[name=vAppraiseFlg]").attr("lay-verify", ""), $("select[name=vFactoryFlg]").attr("lay-verify", ""), $("#teamSelectSubmit").click()));

    form.on('submit(teamAddSubmit)', function ({field}) {
        field.vteamname = field.vteamname.trim()
        let {msg, code} = AjaxFn(url + "teamAdd", JSON.stringify(field), {});
        code == 200 ? (layer.msg(msg, {icon: 1}), teamTableIns.reload(), $("input[name=vteamname]").val(""), $("select[name=vAppraiseFlg]").val(""), $("select[name=vFactoryFlg]").val(""), form.render()): (layer.msg(msg, {icon: 5}))
        return false;
    })

    form.on('submit(teamSelectSubmit)', function ({field}) {
        teamTableIns.reload({
            where: field,
            page: {curr: 1}
        })
        return false;
    })

})({
    form: layui.form,
    table: layui.table,
    layer: layui.layer,
    laydate: layui.laydate,
    tableIns: '',
    positionTableIns: '',
    radioAndAelectData: {},
    tablebtn: function ({event = "", btnclass = "layui-btn btncolor ", icon = "", text = "", disabled = false, btn = ""}) {
        disabled ? disabled = "disabled" : disabled = '';
        btn = `<button type="button" ${disabled} class="${btnclass}" lay-event="${event}"><i class="layui-icon">${icon}</i>${text}</button>`
        return btn;
    },
    teamTableIns: '',
    url: "/evaluation/positionAndTeam/",
    contentType: 'application/json;charset=utf-8',
    ok: function (msg, icon) {
        layui.layer.confirm(msg, {
            id: "ok",
            icon: icon,
            anim: 4,
            title: '提示',
            btn: ['确定'],
            success: function (layero) {
                var btn = layero[0].getElementsByClassName('layui-layer-btn')[0].getElementsByTagName('A')[0];
                btn.href = 'javascript:void(0)';
                btn.focus();
            },
            yes: function (index, layero) {

                layui.layer.close(index);
            },
            end: function () {

            }
        });
    },
    AjaxFn: function (url, data, resource) {
        $.ajax({
            url,
            contentType: 'application/json;charset=utf-8',
            type: 'post',
            async: false,
            data,
            success: function (res) {
                let {msg='',code=''} =res;
                msg=='' && code==''? top.location.reload() : "";
                resource = res;
            }
        });
        return resource;
    },
    startAndForbid: function ({startAndForbid, masg, btns, param, tableIns, url}) {
        layui.layer.confirm(masg, {
            id: "startAndForbid", skin: 'demo-class',
            icon: 3, anim: 4, title: '提示',
            btn: btns,
            success: function (layero) {
                let btn = layero[0].getElementsByClassName('layui-layer-btn')[0].getElementsByTagName('A')[0];
                btn.href = 'javascript:void(0)', btn.focus();
            },
            btn2: function (index, layero) {
                let del_flg = param.delflg
                del_flg == "1" ? param.delflg = 0 : del_flg == "0" ? param.delflg = 1 : "";
                param.startAndForbid = startAndForbid,
                    $.ajax({
                        url: '/evaluation/positionAndTeam/' + url,
                        contentType: 'application/json;charset=utf-8',
                        type: 'post',
                        async: false,
                        data: JSON.stringify(param),
                        success: function (res) {
                            let {msg='',code=''} =res;
                            msg=='' && code==''? top.location.reload() : code == 200 ? icon = 1 : icon = 5;
                            layer.msg(msg, {icon: icon});
                        }
                    })
            },
            end: function () {
                tableIns.reload();
            }
        });
    },
    updataOpen: function ({url, skin, title, tableIns, btns = ['取消', '修改']}) {
        layer.open({
                anim: 4, type: 2, skin: skin, resize: false,
                title: [title, 'font-size:18px; background-color:#e7eaec;'],
                area: ['aotu'],
                btn: btns,
                content: url,
                btn2: function (index, layero) {
                    var body = layer.getChildFrame('body', index);
                    body.find('#submit').click();
                    return false;
                },
                end: function () {
                    tableIns.reload();
                }
            }
        ), $('.' + skin + ' iframe').css('height', '100%'),
            $(".layui-layer-btn").css({
                "padding-top": "0px",
                "padding-bottom": "3px",
                "background-color": "rgb(231, 234, 236)"
            });
    }
})